package org.yxj.requestlib.manager.uploadfile;

import org.yxj.requestlib.i.CurrentUploadFileListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * 上传文件管理类
 *
 * 这里需要使用队列的方式
 */
public class UpLoadFileQueueManager {

    /**
     * 等待消费队列
     */
    private PriorityBlockingQueue<Object> queue;

    /**
     * 当前正在消费队列
     */
    private List<Object> limitList=new ArrayList<>() ;

    /**
     * 回调监听
     *
     */
    private List<CurrentUploadFileListener> listeners = new ArrayList<>();



    private CurrentUploadFileListener currentUploadFileListener ;


    /**
     * 同时上传最大数据
     *
     */
    private int MAX_COUNT = 1 ;

    /**
     * 设置最大消费数
     *
     * @param MAX_COUNT
     */
    public UpLoadFileQueueManager setUpLoadMaxCount(int MAX_COUNT) {
        this.MAX_COUNT = MAX_COUNT;
        return this;
    }


    /**
     * 消费者对象
     */
    private MyThread thread;


    /**
     * 实例化okHttp对象
     */
    private static UpLoadFileQueueManager manager ;



    public static UpLoadFileQueueManager getInstance(){
        if (null==manager){
            synchronized (UpLoadFileQueueManager.class){
                if (null==manager){
                    manager = new UpLoadFileQueueManager();
                }
            }
        }
        return manager ;
    }


    private UpLoadFileQueueManager(){
        queue = new PriorityBlockingQueue();
    }


    /**
     * 添加到队列
     *
     * @param t
     * @param <T>
     */
    public <T> void add(T t){

        /**
         * 压入队列
         *
         */
        if (!queue.contains(t))
        queue.add(t) ;


        /**
         * 等待下载
         */
        if (listeners.size()>0){
            if (currentUploadFileListener!=null){
                currentUploadFileListener.onPing(queue);
            }
        }

        /**
         * 开始消费
         */
        start();
    }



    /**
     * 取出 需要消费任务
     */
    private Object take(){
        try {
            Object t = queue.take();
            return t ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null ;
    }



    /**
     *  启动消费者
     */
    private void start(){
        if (thread==null){
            thread = new MyThread();
            thread.start();
        }
    }



    private class MyThread extends Thread{

        @Override
        public void run() {
            super.run();
            if (queue!=null){
                while (queue.size()>0){
                    if (limitList.size()<MAX_COUNT){

                        Object o = take();

                        if (!limitList.contains(o)){
                            limitList.add(o);
                        }

                        if (currentUploadFileListener!=null){
                            currentUploadFileListener.onUpload(o);
                        }
                    }
                }

                queue.clear();
                limitList.clear();

                interrupt();
                thread = null ;
            }
        }
    }


    /**
     * @param t
     *
     * @param <T>
     */
    public <T> void remove(T t){
        if (limitList.size()>0 && limitList.contains(t)){
            limitList.remove(t);
        }
    }



    /**
     * 添加  上传回调
     *
     * @param currentUploadFileListener
     */
    public void addCurrentUploadFileListener(CurrentUploadFileListener currentUploadFileListener){
        this.currentUploadFileListener=currentUploadFileListener;
    }



    /**
     * 删除当前的监听
     *
     * @param currentUploadFileListener
     */
    public void removeCurrentUploadFileListener(CurrentUploadFileListener currentUploadFileListener){
        if (listeners.contains(currentUploadFileListener))
            listeners.remove(currentUploadFileListener);
    }

}
